<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Manage\Controller;
use Think\Controller;
use Com\UploadFile;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class ManageController extends Controller {

	/* 空操作，用于输出404页面 */
	public function _empty(){
// 		$this->redirect('Index/index');
	}


    protected function _initialize(){
    	
//     	header("charset=utf-8"); 
    	
        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置

        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
        
        $this->login();
        
        //获取数据信息 传值到Index/index控制器
        $this->assign('user',session('user_auth'));
        
        $this->assign('WEB',array('name'=>'微信公众平台'));
        
        
    }

	/* 用户登录检测 */
	protected function login(){
		define('UID',is_login());
		// 还没登录 跳转到登录页面
		if( !UID ){
			$this->redirect('User/index');
		}
		
	}
	
	/**
	 * 文件上传
	 * @param file $file 上传的文件信息  $_FILES['photo']
	 * @return array 返回文件的上传信息
	 */
	protected function fileUpload($file){
		
	    import('Com.UploadFile');
	    
	    $up = new UploadFile(array('allowExts'=>array('jpg','jpeg','png','gif','wmp')));
	    //路径信息
	    $path = '/Upload/'.date('Y',time()).'/'.date('md',time()).'/';
	    
	    $fileinfo = $up->uploadOne($file,ROOT_PATH.'/Public'.$path);
	    
	    $info = $fileinfo[0];
	    
	    /**
	     * $fileinfo 打印
	     Array
	     (
	     [0] => Array
	     (
	     [name] => 20131212110938.png
	     [type] => image/png
	     [size] => 46232
	     [extension] => png
	     [savepath] => E:\APMServ5.2.6\www\htdocs\iyuehu\uploadfile/2013/1212/
	     [savename] => 52a9317b59bec.png
	     [hash] => 53595a2d236cea12c3da4b209ef76954
	     )
	    
	     )
	     */
	    
	    // 	$fileinfo = $up->getUploadFileInfo();
	    $info['uppath'] = $path;
	    // 	dump($info);
	    return $info;
	}
	
	public function ajaxFileUpload(){
		//上传图片信息
		$fileinfo = $this->fileUpload($_FILES['Filedata']);
		if(isset($fileinfo['savename']) && !empty($fileinfo['savename'])){
		    $arr = array(
		        	'imgpath'=>$fileinfo['uppath'].$fileinfo['savename'],
		    		'status'=>1,
		    );
		    $this->ajaxReturn($arr,'JSON');
		}else{
			$arr = array(
					'status'=>0,
			);
			$this->ajaxReturn($arr,'JSON');
		}
	}

}
